// 用户模块路由处理函数

// 引入jwt
const jwt = require('jsonwebtoken')
const secretKey = 'ethink'    // 密钥
const utils = require("../common/utils")
const server = require("../server/user")

// 获取用户信息
exports.getUserInfo = (req, res) => {
  let token = req.get('Authorization').split(' ')[1]
  // token解析
  const data = jwt.verify(token, secretKey)
  data.register_time = utils.formatDate(new Date(data.register_time).getTime())
  return res.send({
    code: 200,
    message: "获取用户信息成功",
    data
  })
}

// 修改用户信息
exports.updateUserInfo = (req, res) => {
  const data = req.body
  server.updateUserInfo(data).then(result => {
    return res.cc("修改用户信息成功", 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改好友备注
exports.setMark = (req, res) => {
  // 好友id
  let id = req.body.id
  let nick_name = req.body.nick_name
  let token = req.get('Authorization').split(' ')[1]
  // 自己的id
  let user_id = jwt.verify(token, secretKey).id
  server.setMark(user_id, id, nick_name).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}